package Day2025_04_11;

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        for (long i = (long) 1e7; i >= 0; i--) {
            String s = i + "";
            int n = s.length();
            long[] arr = new long[n + 1];
            for (int j = 0 ; j < n; j++) {
                int num = Integer.parseInt(s.charAt(j) + "");
                arr[j] = num;
            }
            boolean flag = false;
            long tmp = 0;
            int j = n;
            while (tmp < i) {
                tmp = sum(arr) - arr[j];
                if (tmp == i) flag = true;
                arr[j] = tmp;
                j++;
                if (j == n + 1) j = 0;
            }
            if (flag) {
                System.out.println(i);
                break;
            }
        }
        in.close();
    }
    private static long sum (long[] arr) {
        int n = arr.length;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            sum += arr[i];
        }
        return sum;
    }
}
//7913837